<?php

namespace app\common\library;

use think\Config;
use think\Exception;
use Tx\Mailer;
use Tx\Mailer\Exceptions\CodeException;
use Tx\Mailer\Exceptions\SendException;

/**
 * 蒙古银行支付类
 * @classname RussianPay
 * @package app\common\library
 */
class RussianPay
{

    public string $url;//支付url
    public string $userName;//用户名
    public string $password;//密码

    public function __construct()
    {
        $this->url      = config('site.khanbank_url');
        $this->userName = config('site.khanbank_username');
        $this->password = config('site.khanbank_password');
    }

    /**
     * crul
     * @param $url
     * @param $data
     * @return bool|string
     */
    public function russianCurl($url,$data)
    {
        header("Access-Control-Allow-Origin: *");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_FAILONERROR, 1);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
        curl_setopt($curl, CURLOPT_ENCODING, "UTF-8");
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        $response =  curl_exec($curl);
        curl_close($curl);
        return  json_decode($response,true);

    }

    /**
     * 外蒙银行下单
     * @param $params
     * @return array|false|string|string[]
     */
    public function orderPay($params){
        try {
            $url = $this->url.'payment/rest/register.do';
            $desc = isset($params['body'])??'';
            $language = 'en';
            $currency = '496';
            //通知url
            $returnUrl = config('site.payceshifinish');
            if(!empty($params['returnUrl'])){
                $returnUrl = $params['returnUrl'];
            }
            //失败url
            $failUrl = config('site.failUrl');
            if(!empty($params['failUrl'])){
                $failUrl = $params['failUrl'];
            }
            $data = array(
                'amount'=> $params['amount']*100,
                'currency'=> $currency,
                'language'=> $language,
                'orderNumber' => $params['order_number'],
                'userName' => $this->userName,
                'password' => $this->password,
                'returnUrl' => $returnUrl,
                'failUrl' => $failUrl,
                'description' => $desc,
                'jsonParams' => '{"orderNumber":'.$params['order_number'].'}'
            );
            $response = $this->russianCurl($url,$data);

            if(empty($response['formUrl'])){
                return ['web_url'=>'','msg'=>'获取失败'];
            }
            return ['web_url'=>$response['formUrl'],'out_trade_no'=>$response['orderId'],'msg'=>'ok'] ;
        } catch (Exception $e) {
            return ['web_url'=>'','msg'=>$e->getMessage()];
        }
    }


    /**
     * 查询订单详情
     * @param $orderId
     * @return bool|string
     */
    public function orderInfo($orderId)
    {
        $url = $this->url."payment/rest/getOrderStatus.do";
        $data = array('language'=> 'en',
            'orderId' => $orderId,
            'userName' => $this->userName,
            'password' => $this->password
        );
        return $this->russianCurl($url,$data);
    }

}
